import 'package:flutter/material.dart';

import 'package:get/get.dart';
import 'package:getx_learn/app/common/widgets/widgets.dart';

import '../controllers/fun_response3_controller.dart';

class FunResponse3View extends GetView<FunResponse3Controller> {
  const FunResponse3View({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    print("FunResponse3View 展示UI");
    return Scaffold(
      appBar: getAppBar(context: context, title: "GetBuilder<MyController>()"),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Text("""
            使用 GetBuilder<FunResponse3Controller> 的方式，
            controller 中必须调用 update，GetBuilder<FunResponse3Controller> 这里才会响应执行，
            而且是所有的 GetBuilder<FunResponse3Controller> 都会响应
            
            
            和 GetX 比起来，多了手动控制更新，有两点需要注意。

            1、controller.update(); 触发更新
            2、id: "id2", 标记哪个 builder ，触发方式 controller.update(["id2"]); ，可传多个 Array 类型。
            """),
            GetBuilder<FunResponse3Controller>(
                init: FunResponse3Controller(), // 这里虽然创建了，下面也创建一个新的，但是依旧会使用同一个对象
                // init: controller,
                builder: (controller1) {
                  print(
                      " GetX<FunResponse3Controller> count: ${controller1.hashCode}");
                  return Text(
                    "count的值为: ${controller1.count}",
                    style: TextStyle(color: Colors.red, fontSize: 30),
                  );
                }),
            GetBuilder<FunResponse3Controller>(
              builder: (controller1) {
                print(
                    " GetX<FunResponse3Controller> name: ${controller1.hashCode}");
                return Text(
                  "name的值为: ${controller1.name}",
                  style: TextStyle(color: Colors.red, fontSize: 30),
                );
              },
              init: FunResponse3Controller(),
              // init: controller,
            ),
            GetBuilder<FunResponse3Controller>(
              id: "id2",
              builder: (controller1) {
                print(
                    " GetX<FunResponse3Controller> name: ${controller1.hashCode}");
                return Text(
                  "当前id是id2，: ${controller1.count1}",
                  style: TextStyle(color: Colors.red, fontSize: 30),
                );
              },
              init: FunResponse3Controller(),
              // init: controller,
            ),
            ElevatedButton(
              onPressed: () {
                Get.find<FunResponse3Controller>().increment();
                // controller.increment();
              },
              child: Text("加1"),
            ),
            ElevatedButton(
              onPressed: () {
                Get.find<FunResponse3Controller>().changeName();
                // controller.changeName();
              },
              child: Text("改变名称"),
            ),
            ElevatedButton(
              onPressed: () {
                Get.find<FunResponse3Controller>().increment1();
                // controller.changeName();
              },
              child: Text("只更新 id2 的GetBuilder<FunResponse3Controller>"),
            ),

          ],
        ),
      ),
    );
  }
}
